package cn.edu.hunau.oop.day08;

import cn.edu.hunau.oop.day07.Apple;
import org.junit.Test;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.*;

/**
 * @author hb
 * @date 2025-08-13 14:52:17
 */
public class TestMap {

    @Test
    public void test01(){
        Map<Integer,String> map=new HashMap<>();
        //添加数据
        map.put(1,"张三");
        map.put(2,"李四");
        map.put(3,"王五");
        map.put(4,"赵六");
        map.put(5,"孙七");
        map.put(1,"张三");
        System.out.println(map);
        System.out.println(map.size());
        System.out.println(map.isEmpty());
        System.out.println(map.containsKey(1));
        System.out.println(map.containsValue("王五"));
        String value= map.get(4);
        System.out.println(value);

        //map的遍历
        //方法1
        Set<Integer> keySet = map.keySet();
        for (Integer key : keySet) {
            String v = map.get(key);
            System.out.println(key+"--"+v);
        }
        //方法2
        Set<Map.Entry<Integer, String>> entrySet = map.entrySet();
        for (Map.Entry<Integer, String> entry : entrySet) {
            Integer key = entry.getKey();
            String value1 = entry.getValue();
            System.out.println(key+"--"+value1);
        }
    }

    //abcabc123123123

    @Test
    public void test02(){
        String str="abcab000c1203123123";
        Map<Character,Integer> map=countCharNumbers(str);
        System.out.println(map);

    }
    public Map<Character,Integer> countCharNumbers(String str){
        Map<Character,Integer> map=new HashMap<>();

        for(int i=0;i<str.length();i++){
            char c=str.charAt(i);
            //if(map.containsKey(c)){
            //    map.put(c,map.get(c)+1);
            //}else{
            //    map.put(c,1);
            //}
            Integer value=map.get(c);
            map.put(c,value==null?1:value+1);
        }
        return map;
    }

    @Test
    public void test03(){
        Map<String,Map<String,Double>> map=parseApples(getApplies());
        System.out.println(map);
    }

    public Map<String,Map<String,Double>> parseApples(List<Apple> datas){
        Map<String,Map<String,Double>> map=new HashMap<>();
        //1. 获得一个Map<String,List<Double>>,String对应品种，List<Double>对应每个实例的重量
        Map<String,List<Double>> weightGroup=new HashMap<>();
        for (Apple apple : datas){
            String type=apple.getType();
            List<Double> weightList=weightGroup.getOrDefault(type,new ArrayList<>());
            weightList.add(apple.getWeight());
            weightGroup.put(type,weightList);
        }
        //2.对weightGroup进行处理
        Set<Map.Entry<String, List<Double>>> entries = weightGroup.entrySet();
        for(Map.Entry<String, List<Double>> entry:entries){
            String type=entry.getKey();
            List<Double> weightList=entry.getValue();
            //对weightList进行获得求和，最大值，最小值，平均值的计算
            weightList.sort((a,b)->a.compareTo(b));
            Map<String,Double> countMap=new HashMap<>();
            countMap.put("min",weightList.getFirst());
            countMap.put("max",weightList.getLast());


            double sum=0;
            //获得平均数
            for(Double weight:weightList){
                sum+=weight;
            }
            double avg=sum/weightList.size();

            BigDecimal bd = new BigDecimal(avg);
            //0.123->0.12  0.125->0.13
            BigDecimal newBd = bd.setScale(2, RoundingMode.HALF_UP);

            countMap.put("avg",newBd.doubleValue());
            map.put(type,countMap);
        }
        System.out.println(weightGroup);
        return map;
    }



    public List<Apple> getApplies(){
        List<Apple> appleList=new ArrayList<>();
        // 添加50个Apple对象
        appleList.add(new Apple("红富士", "青色", 167.9));
        appleList.add(new Apple("红富士", "青色", 122.5));
        appleList.add(new Apple("红富士", "青色", 93.7));
        appleList.add(new Apple("红富士", "青色", 192.3));
        appleList.add(new Apple("瑞雪", "红色", 101.2));
        appleList.add(new Apple("瑞雪", "红色", 155.8));
        appleList.add(new Apple("瑞雪", "红色", 138.4));
        appleList.add(new Apple("瑞雪", "红色", 85.6));
        appleList.add(new Apple("红富士", "红色", 112.7));
        appleList.add(new Apple("红富士", "红色", 98.5));
        appleList.add(new Apple("红富士", "红色", 156.3));
        appleList.add(new Apple("红富士", "红色", 189.1));
        appleList.add(new Apple("红富士", "红色", 132.4));
        appleList.add(new Apple("红星", "红色", 179.8));
        appleList.add(new Apple("红星", "绿色", 103.5));
        appleList.add(new Apple("红星", "绿色", 147.2));
        appleList.add(new Apple("红星", "绿色", 88.7));
        appleList.add(new Apple("红星", "绿色", 166.9));
        appleList.add(new Apple("红星", "青色", 133.1));
        appleList.add(new Apple("红星", "青色", 115.4));
        appleList.add(new Apple("红星", "青色", 191.2));
        appleList.add(new Apple("红星", "青色", 99.6));
        appleList.add(new Apple("国光", "红色", 108.3));
        appleList.add(new Apple("国光", "红色", 159.7));
        appleList.add(new Apple("国光", "红色", 136.2));
        appleList.add(new Apple("国光", "红色", 87.5));
        appleList.add(new Apple("国光", "绿色", 172.4));
        appleList.add(new Apple("国光", "绿色", 121.8));
        appleList.add(new Apple("国光", "绿色", 149.3));
        appleList.add(new Apple("国光", "绿色", 95.9));
        appleList.add(new Apple("国光", "青色", 183.6));
        appleList.add(new Apple("国光", "青色", 118.7));
        appleList.add(new Apple("红富士", "绿色", 105.6));
        appleList.add(new Apple("红富士", "绿色", 176.2));
        appleList.add(new Apple("红富士", "绿色", 89.3));
        appleList.add(new Apple("红富士", "绿色", 145.8));
        appleList.add(new Apple("瑞雪", "绿色", 177.3));
        appleList.add(new Apple("瑞雪", "绿色", 112.9));
        appleList.add(new Apple("瑞雪", "绿色", 143.2));
        appleList.add(new Apple("瑞雪", "绿色", 97.8));
        appleList.add(new Apple("瑞雪", "青色", 188.5));
        appleList.add(new Apple("瑞雪", "青色", 126.4));
        appleList.add(new Apple("瑞雪", "青色", 109.1));
        appleList.add(new Apple("瑞雪", "青色", 163.7));
        appleList.add(new Apple("红星", "红色", 92.4));
        appleList.add(new Apple("红星", "红色", 151.6));
        appleList.add(new Apple("红星", "红色", 128.3));
        appleList.add(new Apple("国光", "青色", 164.2));
        appleList.add(new Apple("国光", "青色", 104.5));

        return appleList;
    }
}
